package demo1221.homework;

import java.util.*;

/*
3.已知Student类，属性包括 int id,String name ,
创建5个学生对象存入List集合中，
Student s1 =new Student(1,"a");
Student s2 =new Student(2,"b");
Student s3 =new Student(3,"c");
Student s4 =new Student(4,"d");
Student s5 =new Student(1,"a");
去掉重复元素：当Student类中的name和id值相同时，就是重复元素。
 */
public class Demo_03 {
    public static void main(String[] args) {
        Set<Student> set = new HashSet<>();
        set.add(new Student(1,"a"));
        set.add(new Student(2,"b"));
        set.add(new Student(3,"c"));
        set.add(new Student(4,"d"));
        set.add(new Student(1,"a"));
        List<Student> list = new ArrayList<>(set);
        for (Student student : list) {
            System.out.println(student);
        }


    }
}
class Student{
    private int id;
    private String name;

    public Student(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public Student() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {

        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return id == student.id &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name);
    }
}
